{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'refresh_token': '25.52da95bcf6f6f1b45039f73675ec83da.315360000.1918802086.282335-22866183',\n",
       " 'expires_in': 2592000,\n",
       " 'session_key': '9mzdCytKm2ypaThCtH9e8c6ciDJgpTskyx3iPCaom2EYzKTP4BPgaBIDHEcnrzN2+fKXMHM4tQV7EJCArPjwDG/RzEySmw==',\n",
       " 'access_token': '24.0377f8b94ec266279bd33bddf7b8cd40.2592000.1606034086.282335-22866183',\n",
       " 'scope': 'public brain_all_scope vis-faceverify_faceverify_h5-face-liveness vis-faceverify_FACE_V3 vis-faceverify_idl_face_merge vis-faceverify_FACE_EFFECT vis-faceverify_face_feature_sdk wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component',\n",
       " 'session_secret': '47c4e310f25689d36cbc2d8620bc1ebb'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests \n",
    "\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'\n",
    "client_id = \"7B94tQKN9ngRqlDKz6UVeYpW\"\n",
    "client_secret = \"f6VgAsr9QQ8ZRbZ09FHTarX4ZFHwcyFv\"\n",
    "response = requests.get(host.format(client_id, client_secret))\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 2014520105159,\n",
       " 'timestamp': 1603442293,\n",
       " 'cached': 0,\n",
       " 'result': {'face_num': 1,\n",
       "  'face_list': [{'face_token': 'bb854e7a8ccb02d1e1ad7a9631d0eb32',\n",
       "    'location': {'left': 117.26,\n",
       "     'top': 128.42,\n",
       "     'width': 172,\n",
       "     'height': 180,\n",
       "     'rotation': -11},\n",
       "    'face_probability': 1,\n",
       "    'angle': {'yaw': -6.82, 'pitch': 16.17, 'roll': -11.77},\n",
       "    'face_shape': {'type': 'oval', 'probability': 0.5},\n",
       "    'face_type': {'type': 'human', 'probability': 0.99}}]}}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/detect\"\n",
    "\n",
    "params = \"{\\\"image\\\":\\\"https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1478577125,3953054262&fm=26&gp=0.jpg\\\",\\\"image_type\\\":\\\"URL\\\",\\\"face_field\\\":\\\"faceshape,facetype\\\"}\"\n",
    "\n",
    "access_token = '24.0377f8b94ec266279bd33bddf7b8cd40.2592000.1606034086.282335-22866183' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1478577125,3953054262&fm=26&gp=0.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'error_code': 0,\n",
       " 'error_msg': 'SUCCESS',\n",
       " 'log_id': 7975898925949,\n",
       " 'timestamp': 1603442500,\n",
       " 'cached': 0,\n",
       " 'result': {'score': 36.46869659,\n",
       "  'face_list': [{'face_token': '01ce8206d73e12acced81c955149f807'},\n",
       "   {'face_token': '4331d9080226dbc751b6b4306bdb204b'}]}}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/face/v3/match\"\n",
    "\n",
    "params = \"[{\\\"image\\\": \\\"https://gitee.com/forevercan/resume/raw/master/assets/images/gtboy.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"CERT\\\", \\\"quality_control\\\": \\\"LOW\\\"}, {\\\"image\\\": \\\"https://gitee.com/forevercan/resume/raw/master/assets/images/gtgirl.jpg\\\", \\\"image_type\\\": \\\"URL\\\", \\\"face_type\\\": \\\"LIVE\\\", \\\"quality_control\\\": \\\"LOW\\\"}]\"\n",
    "\n",
    "access_token = '24.0377f8b94ec266279bd33bddf7b8cd40.2592000.1606034086.282335-22866183' # 调用鉴权接口获取的token\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/json'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response.json()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
